﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;

namespace LunchCommand.Models
{
    public class User
    {
        public User()
        {
            this.CreationDate = DateTime.Now;
            this.LastActionTime = this.CreationDate;
            this.LastTimeVenueDecisionMade = this.CreationDate;
        }

        public Int32 UserId { get; set; }

        [Index(IsUnique = true)]
        [MaxLength(255)]
        [MinLength(3)]
        [Required]
        public String WindowsUsername { get; set; }

        [MaxLength(255)]
        [MinLength(3)]
        [Index(IsUnique =true)]
        [Required]
        public String Name { get; set; }

        /// <summary>
        /// Gets or sets all of the decisions the user has made about venues
        /// </summary>
        public virtual ICollection<UserVenueChoice> VenueDecisions { get; set; }

        /// <summary>
        /// Gets or sets all of the decisions the user has made about lunch
        /// </summary>
        public virtual ICollection<LunchUser> LunchesDecided { get; set; }

        /// <summary>
        /// Gets the date this user was created
        /// </summary>
        [Required]
        public DateTime CreationDate { get; private set; }

        /// <summary>
        /// Gets or sets the last time the user has done anything in the system
        /// </summary>
        [Required]
        public DateTime LastActionTime { get; set; }

        /// <summary>
        /// Gets or sets the last time the user upodated their venue choices
        /// </summary>
        [Required]
        public DateTime LastTimeVenueDecisionMade { get; set; }

        /// <summary>
        /// Gets all of the lunches this user has agreed to got o
        /// </summary>
        public IEnumerable<Lunch> LunchesEaten
        {
            get
            {
                return this.LunchesDecided.Where(l => l.Decision).Select(l=>l.Lunch).ToArray();
            }
        }

        /// <summary>
        /// Get all of the venues that this user loves (and not the ones they are indifferent towards or hate)
        /// </summary>
        public IEnumerable<Venue> VenuesPreferring
        {
            get
            {
                if (this.VenueDecisions == null)
                {
                    return new Venue[0];
                }

                return this.VenueDecisions.Where(v => v.Degree == DegreeOfPreference.WantToGo).Select(v => v.Venue);
            }
        }

        /// <summary>
        /// Get all of the venues that this user hates (and not the ones they are indifferent towards or love)
        /// </summary>
        public IEnumerable<Venue> VenuesRefusing
        {
            get
            {
                if (this.VenueDecisions == null)
                {
                    return new Venue[0];
                }

                return this.VenueDecisions.Where(v => v.Degree == DegreeOfPreference.WillNotGo).Select(v => v.Venue);
            }
        }
    }
}